package com.lzj.weather02;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class WeatherReducer extends Reducer<Weather, IntWritable, Text, IntWritable> {
    @Override
    protected void reduce(Weather key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //设置计数器
        List<Integer> days = new ArrayList<>();
        //获取温度的迭代器
        Iterator<IntWritable> iterator = values.iterator();
        //开始遍历
        while (iterator.hasNext()) {
            //要输出的温度
            IntWritable temperature = iterator.next();
            //要输出的日期
            Text outputKey = new Text("区划【" + key.getAdcode() + "】【" + key.getProvince() + ":" + key.getCity() + "】在【" + key.getYear() + "】年【" + key.getMonth() + "】月【" + key.getDay() + "】日");
            //判断是否需要退出
            if (!days.contains(key.getDay()) && days.size() < 3) {
                days.add(key.getDay());
                //将数据写出
                context.write(outputKey, temperature);
            } else {
                //结束Reduce执行
                return;
            }
        }
    }
}